"use strict";

const { Controller } = require("egg");

class AuditController extends Controller {
  // 获取房源数据
  async getAudit() {
    const { ctx } = this;
    const { type, cities, countryCity, secondCity, value } = ctx.query;
    const list = await this.app.mysql.select("audit");
    ctx.body = {
      code: 200,
      list: list
        .filter((v) => {
          if (type == "全部") return true;
          return v.state === type;
        })
        .filter((item) => {
          if (
            cities == "省份" ||
            secondCity == "城市" ||
            countryCity == "县区"
          ) {
            return true;
          }
          return item.city.includes(cities);
        })
        .filter((item) => {
          if (
            cities == "省份" ||
            secondCity == "城市" ||
            countryCity == "县区"
          ) {
            return true;
          }
          return item.city.includes(secondCity);
        })
        .filter((item) => {
          if (
            cities == "省份" ||
            secondCity == "城市" ||
            countryCity == "县区"
          ) {
            return true;
          }
          return item.city.includes(countryCity);
        })
        .filter((v) => {
          if (value == "") return true;
          return v.houses.includes(value);
        }),
    };
  }
  //   审核通过不通过
  async changeAuditList() {
    const { ctx } = this;
    const { auditType, key, houses, city, type1, pic } = ctx.request.body;
    if (auditType == "通过") {
      const post = await this.app.mysql.update(
        "audit",
        { state: "审核通过" },
        { where: { key } }
      );
      if (post.affectedRows === 1) {
        // 审核通过后添加到房源列表/先判断房源数据中有没有当前数据
        const post1 = await this.app.mysql.get("houses", {
          houses,
          type: type1,
          address: city,
        });
        if (!post1) {
          const res = await this.app.mysql.insert("houses", {
            houses,
            address: city,
            pic,
            num: Math.floor(Math.random(100 - 1) + 1),
            date: new Date().toLocaleString(),
            type: type1,
          });
        }
        ctx.body = {
          code: 200,
        };
      } else {
        ctx.body = {
          code: 300,
        };
      }
    } else {
      const post = await this.app.mysql.update(
        "audit",
        { state: "审核不通过" },
        { where: { key } }
      );
      if (post.affectedRows === 1) {
        ctx.body = {
          code: 200,
        };
      } else {
        ctx.body = {
          code: 300,
        };
      }
    }
  }

  // 编辑审核
  async editAudit() {
    const { ctx } = this;
    const { title, pic, size, houses, state, key, type1, address } =
      ctx.request.body;
    const res = await this.app.mysql.update(
      "audit",
      { title, pic, size, houses, state },
      { where: { key } }
    );
    if (state === "审核通过") {
      // 审核通过后添加到房源列表/先判断房源数据中有没有当前数据
      const post1 = await this.app.mysql.get("houses", {
        houses,
        pic,
        address,
        type: type1,
      });
      if (!post1) {
        const res = await this.app.mysql.insert("houses", {
          houses,
          address,
          pic,
          num: Math.floor(Math.random(100 - 1) + 1),
          date: new Date().toLocaleString(),
          type: type1,
        });
      }
    }else{
            // 审核不通过删除房源中的
            const post1 = await this.app.mysql.get("houses", {
              houses,
              pic,
              address,
              type: type1,
            });
            if (post1) {
              const res = await this.app.mysql.delete("houses", {
                houses,
                address,
                pic,
                type: type1,
              });
            }
    }
    if (res.affectedRows === 1) {
      ctx.body = {
        code: 200,
      };
    } else {

      ctx.body = {
        code: 300,
      };
    }
  }
}

module.exports = AuditController;
